<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>数据表管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				},
				rules:{
					name:{
						remote: {
						    url: $("#ctx").val()+"/ins/insTable/getByName",     //后台处理程序
						    type: "post",               //数据发送方式
						    dataType: "json",           //接受数据格式   
						    data: {                     //要传递的数据
						        name: function() {
						            return $("#name").val();
						        },
						        id: function(){
						        	return $("#id").val();
						        }
						    }
						}
					}
				},
				messages:{
					name:{
						remote:"该数据表名称已存在！"
					}
				}
			});
		});
		function addRow(list, idx, tpl, row){
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
				var ss = $(this).attr("data-value").split(',');
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
		}
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var rid = $(prefix+"_rid");
			var delFlag = $(prefix+"_delFlag");
			var frequently = $(prefix+"_frequently");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				delFlag.val("1");
				$(obj).html("&divide;").attr("title", "撤销删除");
				$(obj).parent().parent().addClass("error");
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
			//删除常用字段的select选项
			if(frequently.val()=="1"){
				$("#fieldList option[value="+rid.val()+"]").attr("selected", "");
			}
		}
	</script>
</head>
<body>
	<form:form id="inputForm" modelAttribute="insTable" action="${ctx}/ins/insTable/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input type="hidden" id="ctx" value="${ctx}"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">主名称：</label>
			<div class="controls">
				<form:input path="name" disabled="disabled" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">次名称：</label>
			<div class="controls">
				<form:input path="fullName" disabled="disabled" htmlEscape="false" maxlength="64" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">分类：</label>
			<div class="controls">
			<sys:treeselect disabled="disabled" id="parent" name="category.id" value="${insTable.category.id}" labelName="insTablecategory.name" labelValue="${insTable.category.name}"
					title="父级分类" url="/ins/insCategory/treeData" extId="${insCategory.id}" cssClass="" allowClear="true" dataMsgRequired="分类必选"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<%-- <div class="control-group">
			<label class="control-label">排序：</label>
			<div class="controls">
				<form:input path="sort" htmlEscape="false" value="10" class="input-xlarge required"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="1000" class="input-xxlarge "/>
			</div>
		</div> --%>
			<div class="control-group">
				<label class="control-label">参数列表：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>参数名称</th>
								<th>占位符</th>
								<th>参数类型</th>
								<th>预录入</th>
								<th>排序</th>
								<th>默认值</th>
								<th>必填</th>
								<th>唯一</th>
								<th>索引</th>
							</tr>
						</thead>
						<tbody id="insFieldList">
						</tbody>
					</table>
					<script type="text/template" id="insFieldTpl">//<!--
						<tr id="insFieldList{{idx}}">
							<td class="hide">
								<input id="insFieldList{{idx}}_id" name="insFieldList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="insFieldList{{idx}}_rid" class="rid" type="hidden" value="{{row.rid}}"/>
								<input id="insFieldList{{idx}}_frequently" type="hidden" value="{{row.frequently}}"/>
								<input id="insFieldList{{idx}}_delFlag" name="insFieldList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input disabled="disabled" id="insFieldList{{idx}}_name" name="insFieldList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="64" class="input-mini required"/>
							</td>
							<td>
								<input disabled="disabled" id="insFieldList{{idx}}_code" name="insFieldList[{{idx}}].code" type="text" value="{{row.code}}" maxlength="64" class="input-mini" onkeyup="this.value=this.value.replace(/[^\w_]/g,'');"/>
							</td>
							<td>
								<!--<input id="insFieldList{{idx}}_type" name="insFieldList[{{idx}}].type" type="text" value="{{row.type}}" maxlength="2" class="input-small required"/>-->
								<select disabled="disabled" id="insFieldList{{idx}}_type" name="insFieldList[{{idx}}].type" data-value="{{row.type}}" class="input-small required">
									<option value="1">字符串</option>
									<option value="2">数字</option>
									<option value="3">年月日时分秒</option>
									<option value="4">年月日</option>
								</select>
							</td>
							<td>
								<span><input disabled class="preInput" checked id="insFieldList{{idx}}_preInput0" name="insFieldList[{{idx}}].preInput" type="radio" value="0" data-value="{{row.preInput}}"><label for="insFieldList{{idx}}_preInput0">录入</label></span>
								<span><input disabled class="preInput" id="insFieldList{{idx}}_preInput1" name="insFieldList[{{idx}}].preInput" type="radio" value="1" data-value="{{row.preInput}}"><label for="insFieldList{{idx}}_preInput1">选择</label></span>
								<span><textarea disabled id="insFieldList{{idx}}_preValues" name="insFieldList[{{idx}}].preValues" rows="1" maxlength="1000" class="input-small ">{{row.preValues}}</textarea></span>
							</td>
							<td>
								<input disabled="disabled" id="insFieldList{{idx}}_sort" name="insFieldList[{{idx}}].sort" type="text" value="{{row.sort}}" class="input-mini " onkeyup="this.value=this.value.replace(/[^\d]/g,'');"/>
							</td>
							<td>
								<input disabled="disabled" id="insFieldList{{idx}}_defaultValue" name="insFieldList[{{idx}}].defaultValue" type="text" value="{{row.defaultValue}}" maxlength="64" class="input-mini"/>
							</td>
							<td>
								<div><input class="dis" disabled="disabled" checked id="r{{idx}}0" name="insFieldList[{{idx}}].required" type="radio" value="0" data-value="{{row.required}}"><label for="insFieldList{{idx}}_required0">非必填</label></div>
								<div><input class="dis" disabled="disabled" id="r{{idx}}1" name="insFieldList[{{idx}}].required" type="radio" value="1" data-value="{{row.required}}"><label for="insFieldList{{idx}}_required1">必填</label></div>
							</td>
							<td>
								<div><input class="dis" disabled="disabled" checked id="u{{idx}}0" name="insFieldList[{{idx}}].uniquely" type="radio" value="0" data-value="{{row.uniquely}}"><label for="insFieldList{{idx}}_uniquely0">可重复</label></div>
								<div><input class="dis" disabled="disabled" id="u{{idx}}1" name="insFieldList[{{idx}}].uniquely" type="radio" value="1" data-value="{{row.uniquely}}"><label for="insFieldList{{idx}}_uniquely1">唯一</label></div>
							</td>
							<td>
								<input type="checkbox" disabled="disabled" ind="{{idx}}" value="1" data-value="{{row.indexed}}" class="indexed"/>
								<input type="hidden" disabled="disabled" class="hidden" value="{{row.indexed}}" name="insFieldList[{{idx}}].indexed"/>
							</td>
						</tr>//-->
					</script>
					<script type="text/javascript">
						/* if($("#id").val()!=""){
							$("#selectSpan").hide();
						} */
						var insFieldRowIdx = 0, insFieldTpl = $("#insFieldTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							$("#fieldList").select2({
								placeholder: "常用参数"
							});
							var fieldList = ${fns:toJson(fieldList)};
							var data = ${fns:toJson(insTable.insFieldList)};
							$("#fieldList").on("select2-selecting",function(e){
								for(var i=0;i<fieldList.length;i++){
									if(e.val == fieldList[i].id){
										var row = jQuery.extend(true, {}, fieldList[i]); 
										row.rid=row.id;
										row.id="";
										addRow('#insFieldList', insFieldRowIdx, insFieldTpl, row);
										insFieldRowIdx = insFieldRowIdx + 1;
									}
								}
							});
							$("#fieldList").on("select2-removing",function(e){
								$(".rid").each(function(){
									if($(this).val()==e.val){
										$(this).parent().parent().remove();
									}
								});
							});
							for (var i=0; i<data.length; i++){
								addRow('#insFieldList', insFieldRowIdx, insFieldTpl, data[i]);
								insFieldRowIdx = insFieldRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
	</form:form>
</body>
</html>